<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Files</title>
<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../overview.html" title="Overview">
<link rel="prev" href="timers.html" title="Timers">
<link rel="next" href="pipes.html" title="Pipes">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
<td align="center"><a href="../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="timers.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="pipes.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_asio.overview.files"></a><a class="link" href="files.html" title="Files">Files</a>
</h3></div></div></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          This feature requires I/O completion ports on Windows, and io_uring on
          Linux (define <code class="computeroutput"><span class="identifier">BOOST_ASIO_HAS_IO_URING</span></code>
          to enable).
        </p></td></tr>
</table></div>
<p>
        Boost.Asio provides support for manipulating stream-oriented and random-access
        files. For example, to write to a newly created stream-oriented file:
      </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">stream_file</span> <span class="identifier">file</span><span class="special">(</span>
    <span class="identifier">my_io_context</span><span class="special">,</span> <span class="string">"/path/to/file"</span><span class="special">,</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">stream_file</span><span class="special">::</span><span class="identifier">write_only</span>
      <span class="special">|</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">stream_file</span><span class="special">::</span><span class="identifier">create</span>
      <span class="special">|</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">stream_file</span><span class="special">::</span><span class="identifier">truncate</span><span class="special">);</span>

<span class="identifier">file</span><span class="special">.</span><span class="identifier">async_write_some</span><span class="special">(</span><span class="identifier">my_buffer</span><span class="special">,</span>
    <span class="special">[](</span><span class="identifier">error_code</span> <span class="identifier">e</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">n</span><span class="special">)</span>
    <span class="special">{</span>
      <span class="comment">// ...</span>
    <span class="special">});</span>
</pre>
<p>
        or to read from a random-access file:
      </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">random_access_file</span> <span class="identifier">file</span><span class="special">(</span>
    <span class="identifier">my_io_context</span><span class="special">,</span> <span class="string">"/path/to/file"</span><span class="special">,</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">random_access_file</span><span class="special">::</span><span class="identifier">read_only</span><span class="special">);</span>

<span class="identifier">file</span><span class="special">.</span><span class="identifier">async_read_some_at</span><span class="special">(</span><span class="number">1234</span><span class="special">,</span> <span class="identifier">my_buffer</span><span class="special">,</span>
    <span class="special">[](</span><span class="identifier">error_code</span> <span class="identifier">e</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">n</span><span class="special">)</span>
    <span class="special">{</span>
      <span class="comment">// ...</span>
    <span class="special">});</span>
</pre>
<h5>
<a name="boost_asio.overview.files.h0"></a>
        <span class="phrase"><a name="boost_asio.overview.files.see_also"></a></span><a class="link" href="files.html#boost_asio.overview.files.see_also">See
        Also</a>
      </h5>
<p>
        <a class="link" href="../reference/basic_file.html" title="basic_file">basic_file</a>, <a class="link" href="../reference/basic_random_access_file.html" title="basic_random_access_file">basic_random_access_file</a>,
        <a class="link" href="../reference/basic_stream_file.html" title="basic_stream_file">basic_stream_file</a>,
        <a class="link" href="../reference/file_base.html" title="file_base">file_base</a>, <a class="link" href="../reference/random_access_file.html" title="random_access_file">random_access_file</a>,
        <a class="link" href="../reference/stream_file.html" title="stream_file">stream_file</a>.
      </p>
</div>
<div class="copyright-footer">Copyright © 2003-2024 Christopher M. Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="timers.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="pipes.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
